package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.DeleteRequest;
import com.yupi.springbootinit.mapper.QaInspectionReportDimMapper;
import com.yupi.springbootinit.model.dto.qa.QaInspectionReportDimAddRequest;
import com.yupi.springbootinit.model.dto.qa.QaInspectionReportDimQueryRequest;
import com.yupi.springbootinit.model.dto.qa.QaInspectionReportDimUpdateRequest;
import com.yupi.springbootinit.model.entity.QaInspectionReportDim;
import com.yupi.springbootinit.service.QaInspectionReportDimService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author zlc
* @description 针对表【qa_inspection_report_dim(主尺寸检验条目)】的数据库操作Service实现
* @createDate 2024-07-23 16:17:31
*/
@Service
public class QaInspectionReportDimServiceImpl extends ServiceImpl<QaInspectionReportDimMapper, QaInspectionReportDim>
    implements QaInspectionReportDimService{

    @Override
    public long addQaInspectionReportDim(QaInspectionReportDimAddRequest qaInspectionReportDimAddRequest) {
        QaInspectionReportDim qaInspectionReportDim = new QaInspectionReportDim();
        BeanUtils.copyProperties(qaInspectionReportDimAddRequest, qaInspectionReportDim);
        this.save(qaInspectionReportDim);
        return qaInspectionReportDim.getId();
    }

    @Override
    public boolean deleteQaInspectionReportDim(DeleteRequest deleteRequest) {
        long id = deleteRequest.getId();
        return this.removeById(id);
    }

    @Override
    public boolean updateQaInspectionReportDim(QaInspectionReportDimUpdateRequest qaInspectionReportDimUpdateRequest) {
        QaInspectionReportDim qaInspectionReportDim = new QaInspectionReportDim();
        BeanUtils.copyProperties(qaInspectionReportDimUpdateRequest, qaInspectionReportDim);
        return this.updateById(qaInspectionReportDim);
    }

    @Override
    public QaInspectionReportDim getQaInspectionReportDimById(long id) {
        return this.getById(id);
    }

    @Override
    public List<QaInspectionReportDim> listQaInspectionReportDim(QaInspectionReportDimQueryRequest qaInspectionReportDimQueryRequest) {
        QueryWrapper<QaInspectionReportDim> queryWrapper = getQueryWrapper(qaInspectionReportDimQueryRequest);
        return this.list(queryWrapper);
    }

    @Override
    public Page<QaInspectionReportDim> listQaInspectionReportDimByPage(QaInspectionReportDimQueryRequest qaInspectionReportDimQueryRequest) {
        long current = qaInspectionReportDimQueryRequest.getCurrent();
        long size = qaInspectionReportDimQueryRequest.getPageSize();
        Page<QaInspectionReportDim> page = new Page<>(current, size);
        QueryWrapper<QaInspectionReportDim> queryWrapper = getQueryWrapper(qaInspectionReportDimQueryRequest);
        return this.page(page, queryWrapper);
    }

    private QueryWrapper<QaInspectionReportDim> getQueryWrapper(QaInspectionReportDimQueryRequest qaInspectionReportDimQueryRequest) {
        QueryWrapper<QaInspectionReportDim> queryWrapper = new QueryWrapper<>();
        if (qaInspectionReportDimQueryRequest == null) {
            return queryWrapper;
        }
        Long id = qaInspectionReportDimQueryRequest.getId();
        if (id != null && id > 0) {
            queryWrapper.eq("id", id);
        }
        return queryWrapper;
    }
}